import pytest
import yaml
from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class Base:
	_base_url = ''
	_path = ''
	_driver = None

	def __init__(self, driver: WebDriver = None, reuse=False):
		if driver is None:
			if reuse:
				options = webdriver.ChromeOptions()
				options.debugger_address = '127.0.0.1:9222'
				self._driver = webdriver.Chrome(options=options)
			else:
				self._driver = webdriver.Chrome()
			self._driver.implicitly_wait(6)
		if self._base_url != '':
			print(self._base_url)
			self._driver.get(self._base_url)


	@classmethod
	def set_base_url(cls, _base_url, _path=''):
		Base._base_url = _base_url + _path

	@classmethod
	def get_base_url(cls):
		return Base._base_url

	def find(self, by, locator=None):
		if isinstance(by, tuple):
			return self._driver.find_element(*by)
		else:
			return self._driver.find_element(by, locator)

	def find_wait(self, by, locator=None):
		if isinstance(by, tuple):
			WebDriverWait(self._driver, 10).until(expected_conditions.element_to_be_clickable(by))
			return self._driver.find_element(*by)
		else:
			WebDriverWait(self._driver, 10).until(expected_conditions.element_to_be_clickable((by, locator)))
			return self._driver.find_element(by, locator)








